package com.blockmeta.bbs.businesslibrary.widget.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import androidx.annotation.ColorInt;
import android.util.AttributeSet;

import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;

/**
 * @author AirThor
 * @date 2020/2/20.
 * 重写RadioButton 来实现IM底部tab展示消息未读数
 */
public class MessageRadioButton extends DrawableRadioButton {

    private RectF mBubbleRect = new RectF();
    private Rect mTextBounds = new Rect();

    private Paint mBubblePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private @ColorInt int mStrokeColor = Color.WHITE;
    private @ColorInt int mFillColor = Color.RED;
    private @ColorInt int mTextColor = Color.WHITE;

    private float mLeft,mTop,mRight, mBottom;
    private final int DP_1 = DimentionsUtil.dp2px(1);
    private final int DP_10 = DimentionsUtil.dp2px(10);
    private final int DP_6 = DimentionsUtil.dp2px(6);

    private int mMessageCount = 0;
    public MessageRadioButton(Context context) {
        super(context);
        initialize(context);
    }

    public MessageRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }

    private void initialize(Context context){
        mTextPaint.setTextSize(DimentionsUtil.dp2px(context,12));
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextAlign(Paint.Align.CENTER);

        mBubblePaint.setStrokeWidth(DP_1);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mLeft = w/2f;
        mTop = getPaddingTop();
        mBottom = mTop+DimentionsUtil.dp2px(18);
    }

    @Override
    public void onDrawForeground(Canvas canvas) {
        super.onDrawForeground(canvas);
        if (mMessageCount<=0){return;}
        String count = getMessageCount();
        mTextPaint.getTextBounds(count,0,count.length(),mTextBounds);
        float bubbleHeight = mBottom -mTop;
        mRight = (mTextBounds.width()+DP_6<bubbleHeight?bubbleHeight:mTextBounds.width()+DP_10)+mLeft;
        //画气泡边框
        mBubblePaint.setStyle(Paint.Style.STROKE);
        mBubblePaint.setColor(mStrokeColor);
        mBubbleRect.set(mLeft,mTop,mRight,mBottom);
        canvas.drawRoundRect(mBubbleRect,bubbleHeight/2f,bubbleHeight/2f,mBubblePaint);
        //画气泡
        mBubblePaint.setStyle(Paint.Style.FILL);
        mBubblePaint.setColor(mFillColor);
        mBubbleRect.set(mLeft+DP_1,mTop+DP_1,mRight-DP_1,mBottom-DP_1);
        float r = (bubbleHeight-DP_1-DP_1)/2f;
        canvas.drawRoundRect(mBubbleRect,r,r,mBubblePaint);
        //画出文字
        float x = mLeft+(mRight-mLeft)/2f;
        float y = mTop+DP_1+(bubbleHeight-mTextPaint.descent()+mTextPaint.ascent())/2-mTextPaint.ascent();
        canvas.drawText(count,x,y,mTextPaint);
    }

    public String getMessageCount() {
        return mMessageCount>99?"99+":String.valueOf(mMessageCount);
    }

    public void setMessageCount(int messageCount) {
        if (mMessageCount==messageCount){
            return;
        }
        mMessageCount = messageCount;
        postInvalidate();
    }
}
